using Engine.DynamicObjectManager;
using SharpDX;
using System.IO;

namespace WorldRacing2.Scenario.WR2TerrainLoader.QadFile
{
    public class Object : BaseObject
    {
        #region Private Fields

        private byte[] keineAhnung4 = new byte[8];
        private int objectPropertieID;
        private Matrix rotationsmatrix;

        #endregion Private Fields

        #region Public Constructors

        public Object(string name, int ObjectPropertyId, float x, float y, float z)
        {
            this.AngleY = 0.0f;
            byte[] keineAhnung4 = new byte[8];
            base.Name = name;
            this.objectPropertieID = ObjectPropertyId;
            this.PositionX = x;
            this.PositionY = y;
            this.PositionZ = z;
            this.Size = 1.0f;
        }

        public Object()
        {
            this.AngleY = 0.0f;
            byte[] keineAhnung4 = new byte[8];
            this.Name = "not defined!";
            this.objectPropertieID = 0;
            this.PositionX = 0.0f;
            this.PositionY = 0.0f;
            this.PositionZ = 0.0f;
            this.Size = 1.0f;
        }

        #endregion Public Constructors

        #region Public Methods

        public override string ToString()
        {
            return base.Name.ToString();
        }

        #endregion Public Methods

        #region Internal Methods

        internal new static Object FromStream(BinaryReader br)
        {
            Object obj = new Object();
            obj.Name = ObjectName.FromStream(br).ToString();
            obj.objectPropertieID = br.ReadInt32();
            obj.PositionX = br.ReadSingle();
            obj.PositionY = br.ReadSingle();
            obj.PositionZ = br.ReadSingle();

            obj.AngleY = br.ReadSingle();
            obj.Size = br.ReadSingle();

            obj.rotationsmatrix = new Matrix();
            obj.rotationsmatrix.M11 = br.ReadSingle();
            obj.rotationsmatrix.M12 = br.ReadSingle();
            obj.rotationsmatrix.M13 = br.ReadSingle();
            obj.rotationsmatrix.M21 = br.ReadSingle();
            obj.rotationsmatrix.M22 = br.ReadSingle();
            obj.rotationsmatrix.M23 = br.ReadSingle();
            obj.rotationsmatrix.M31 = br.ReadSingle();
            obj.rotationsmatrix.M32 = br.ReadSingle();
            obj.rotationsmatrix.M33 = br.ReadSingle();

            obj.keineAhnung4 = br.ReadBytes(8);

            return obj;
        }

        internal void Save(BinaryWriter bw)
        {
            ObjectName objectName = new ObjectName(this.Name);
            objectName.Save(bw);
            bw.Write(this.objectPropertieID);
            bw.Write(this.PositionX);
            bw.Write(this.PositionY);
            bw.Write(this.PositionZ);
            bw.Write(this.AngleY);
            bw.Write(this.Size);

            this.rotationsmatrix = Matrix.RotationY(this.AngleY);

            bw.Write(this.rotationsmatrix.M11);
            bw.Write(this.rotationsmatrix.M12);
            bw.Write(this.rotationsmatrix.M13);
            bw.Write(this.rotationsmatrix.M21);
            bw.Write(this.rotationsmatrix.M22);
            bw.Write(this.rotationsmatrix.M23);
            bw.Write(this.rotationsmatrix.M31);
            bw.Write(this.rotationsmatrix.M32);
            bw.Write(this.rotationsmatrix.M33);
            bw.Write(this.keineAhnung4);
        }

        #endregion Internal Methods
    }
}